package com.newer.his.common.mapper;

import java.sql.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.newer.his.common.entity.Dept;
import com.newer.his.common.entity.Doctor;
import com.newer.his.common.entity.Working;


/**
 * 排班记录
 * @author Tang
 *
 */


@Mapper
public interface WokingMapper {
	
	//插入一条排班记录
	@Options(useGeneratedKeys = true,keyColumn = "woking_id",keyProperty = "woking_id")
	@Insert("INSERT INTO `working`(`sdate`,`id_dept`,`id_doctor`,`workTime`,`emerTime`, `rank` ) VALUES ( #{sdate}, #{id_dept.id}, 4, #{id_doctor.id}, #{emertime}, {rank});")
	 Working save(Working woking) ;
	
	
	//查询一个星期的排班
	@Results(
			id = "",
			value = {
		@Result(  property= "sortid",column = "woking_id"),
		@Result(  property= "iddept",column = "id_dept", 
				  javaType = Dept.class,
						  one= @One(select="com.newer.his.common.mapper.DeptMapper.findbyId")),
		
		@Result(  property= "iddoctor",column = "id_doctor",
				  javaType = Doctor.class,	
		          one= @One(select="com.newer.his.common.mapper.DoctorMapper.findById"))
	})
	@Select("select woking_id,sdate,sdate,id_dept,id_doctor,workTime,emerTime,`rank` from working where sdate between #{date} and DATE_ADD(#{date},INTERVAL 7 DAY) and id_dept;")
	 List<Working> getwokingByDay(Date date,int deptid);
	
	
	
	
	//查询某一天的排班
	@Results(
			id = "",
			value = {
		@Result(  property= "sortid",column = "woking_id"),
		@Result(  property= "iddept",column = "id_dept", 
				  javaType = Dept.class,
						  one= @One(select="com.newer.his.common.mapper.DeptMapper.findbyId")),
		
		@Result(  property= "iddoctor",column = "id_doctor",
				  javaType = Doctor.class,	
		          one= @One(select="com.newer.his.common.mapper.DoctorMapper.findById"))
	})
	@Select("	select woking_id,sdate,sdate,id_dept,id_doctor,workTime,emerTime,`rank` from working where sdate ='date';")
	 List<Working> getwokingoneDay(Date date);
	
	
	
	
	
	
	
	
	//修改排班状态  通过医生id 和时间 来修改
	@Update("update working   set workTime=#{worktime},emerTime=#{emertime} where id_doctor=#{iddoctor.id} and sdate= #{sdate};")
	int  putwokingByDay(Working working);
	


	
	
}
